<?php

namespace Admin\Controller;
use Couchbase\ViewQuery;
use Think\Controller;

class ApiController extends ApiCommonController
{
    function __construct()
    {
        parent::__construct();
    }
    //登录
    public function login()
    {
        $data_mm = I('data');
        $data_jm = $this->jiemi($data_mm);
        $info = D('Admin');
        $phone = $data_jm['phone'];
        $p = $data_jm['pwd'];
        $pwd = set_pwd($p, $phone);
        $res = $info->check_login($phone, $pwd);
        if ($res) {
            $data['kefu_id'] = $res['id'];
            $data['name'] = $res['name'];
            $data['phone'] = $res['phone'];
            $admin = M('admin', '', 'KEFU')->where(['phone' => $phone])->Field('part_id,login_status')->find();
            $data['part_id'] = $admin['part_id'];
            $data['login_status'] = $admin['login_status'];
            add_log($res['name'] . $res['id'] . '在手机端登录');
            $data = $this->jiami(['code' => '1001', 'result' => 'success', 'data' => $data]);
            $this->ajaxReturn(['code' => '1001', 'data' => $data]);
            die;
        }
        $data = $this->jiami(['code' => '1002', 'result' => '用户名或密码错误！', 'data' => '']);
        $this->ajaxReturn(['code' => '1001', 'data' => $data]);
        die;
    }

    //通讯录
    public function user_contact()
    {
        $data_mm = I('data');
        $data_jm = $this->jiemi($data_mm);
        $page = $data_jm['page'];
        $data = M('admin', '', 'KEFU')->join('a join dxd_kefu.apart b on a.part_id=b.id ')->where(['a.status' => '1'])->field('a.id as admin_id,a.name as admin_name,b.name as part_name')->order('b.id desc')->limit($page, 10)->select();
        $page = $page + count($data);

        $is_null = empty($data) ? 1 : 0;
        $data = $this->jiami(['code' => '1001', 'result' => 'success', 'is_null' => $is_null, 'page' => $page, 'data' => $data]);
        $this->ajaxReturn(['code' => '1001', 'is_null' => $is_null, 'data' => $data]);
        die;

    }

    //客服个人信息
    public function kf_personal()
    {
        $data_mm = I('post.data');
        if (empty($data_mm)) {
            $this->ajaxReturn(['code' => '1002', 'result' => '密文为空']);
            die;
        }
        $data_jm = $this->jiemi($data_mm);
        if (empty($data_jm)) {
            $this->ajaxReturn(['code' => '1002', 'result' => '解密错误']);
        }

        $admin_id = $data_jm['admin_id'];
        $field = "a.id as admin_id,a.phone,a.name as admin_name,a.sex,a.email,a.level,a.agent,b.name as part_name,c.name as role_name";
        $data = M('admin', '', 'KEFU')->join('a join dxd_kefu.apart b on a.part_id=b.id')->join('dxd_kefu.role c on a.role_id=c.id')->where(['a.id' => $admin_id])->field($field)->find();
        $data_arr[] = array('name_k' => '姓名', 'name_v' => $data['admin_name']);
        $data_arr[] = array('name_k' => '工号', 'name_v' => $data['admin_id']);
        $data_arr[] = array('name_k' => '性别', 'name_v' => $data['sex']);
        $data_arr[] = array('name_k' => '手机', 'name_v' => $data['phone']);
        $data_arr[] = array('name_k' => '座机', 'name_v' => $data['agent']);
        $data_arr[] = array('name_k' => '邮箱', 'name_v' => $data['email']);
        $data_arr[] = array('name_k' => '所属部门', 'name_v' => $data['part_name']);
        $data_arr[] = array('name_k' => '职务', 'name_v' => $data['level']);
        $data_arr[] = array('name_k' => '级别', 'name_v' => $data['level']);
        $data = $this->jiami(['code' => '1001', 'result' => 'success', 'data' => $data_arr]);
        $this->ajaxReturn(['code' => '1001', 'result' => 'success', 'data' => $data]);
        die;

    }

    public function push()
    {
        $app_key = 'dba87c6c10ab941f1e287026'; // appkey
        $master_secret = '0eb2ff195817d4b43915c2ac'; // master_secret
        $client = new \Admin\Service\JPush\Client($app_key, $master_secret);
        $pusher = $client->push();
        $pusher->setPlatform(array('ios', 'android'));
        $pusher->options(['apns_production' => true]);
        $pusher->addTagAnd();
        $pusher->setNotificationAlert('你有新的工单需要处理');
        try {
            $pusher->send();
        } catch (\Admin\Service\JPush\Exceptions\JPushException $e) {
            print $e;
        }
    }

    //登出
    public function login_out()
    {
        $data_mm = I('data');
        $data_jm = $this->jiemi($data_mm);
        $admin = $data_jm['admin_id'];
        $update = M('admin', '', 'KEFU')->where(['id' => $admin])->setField('login_status', 2);
        if ($update) {
            $data = $this->jiami(['code' => '1001', 'result' => '登出成功', 'data' => '']);
            $this->ajaxReturn(['code' => '1001', 'data' => $data]);
            die;
        } else {
            $data = $this->jiami(['code' => '1002', 'result' => '登出失败', 'data' => '']);
            $this->ajaxReturn(['code' => '1001', 'data' => $data]);
            die;
        }


    }

    //添加工单
    public function add_work_order()
    {
        $data_mm = I('data');
        $data_jm = $this->jiemi($data_mm);
        if (empty($data_jm)) {
            $this->ajaxReturn(['code' => '1001', 'result' => '密文为空', 'data' => '']);
            die;
        }
        $part_id = $data_jm['part_id'];
        $admin_name = $data_jm['admin_name'];
        $accept = $data_jm['accept_part_id'];
        $order_level = $data_jm['order_level'];
        $title = $data_jm['title'];
        $content = $data_jm['content'];
        $images = $data_jm['images'];//链接以’,’隔开
        $arr = array('apart_id' => $part_id, 'publish_name' => $admin_name, 'accept_apart_id' => $accept, 'title' => $title, 'ctime' => time(), 'level' => $order_level);
        $add = M('work_order', '', 'KEFU')->add($arr);
        $arr1 = array('content' => $content, 'img' => $images, 'wo_id' => $add);
        $add1 = M('work_order_detail', '', 'KEFU')->add($arr1);
        if ($add && $add1) {
            $data = $this->jiami(['code' => '1001', 'result' => '添加成功']);
            $app_key = 'dba87c6c10ab941f1e287026'; // appkey
            $master_secret = '0eb2ff195817d4b43915c2ac'; // master_secret
            $client = new \Admin\Service\JPush\Client($app_key, $master_secret);
            $pusher = $client->push();
            $pusher->setPlatform(array('ios', 'android'));
            $pusher->options(['apns_production' => true]);
            $pusher->addTagAnd($accept);
            $pusher->setNotificationAlert('你有新的工单需要处理');
            try {
                $pusher->send();
            } catch (\Admin\Service\JPush\Exceptions\JPushException $e) {
                print $e;
            }
        } else {
            $data = $this->jiami(['code' => '1002', 'result' => '添加失败']);
        }
        $this->ajaxReturn(['code' => '1001', 'data' => $data]);
        die;
    }

    //工单
    public function work_order_list()
    {
        $data_mm = I('data');
        $data_jm = $this->jiemi($data_mm);
        if (empty($data_jm)) {
            $this->ajaxReturn(['code' => '1002', 'result' => '密文为空']);
        }
        $status = $data_jm['status'];//1 未完成  2 已完成
        $page = $data_jm['page'];
        $part = $data_jm['part_id'];
        $is_null = 0;
        $where = [];
        $w['accept_apart_id'] = $part;
        $w['apart_id'] = $part;
        $w['_logic'] = "OR";
        if ($status == '2') {
            $where['status'] = array('in', array(3, 4));
        } else {
            $where['status'] = array('in', array(1, 2));
        }
        $where['_complex'] = $w;
        $where['_logic'] = "AND";
        $field = "id as order_id,publish_name,ctime,status,type,accept_apart_id,upd_time,level";
        $data = M('work_order', '', 'KEFU')->where($where)->limit($page, 10)->order('ctime desc,status asc')->field($field)->select();
        $page = $page + count($data);
        if (empty($data)) {
            $is_null = 1;
            $data1 = $this->jiami(['code' => '1001', 'result' => 'success', 'page' => $page, 'is_null' => $is_null, 'data' => $data]);
            $this->ajaxReturn(['code' => 1001, 'data' => $data1]);
            die;
        }

        foreach ($data as $k => $v) {
            $data[$k]['accept_apart'] = M('apart', '', 'KEFU')->where(['id' => $v['accept_apart_id']])->getField('name');
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if (!empty($data[$k]['upd_time'])) {
                $data[$k]['upd_time'] = date('Y-m-d H:i:s', $v['upd_time']);
            } else {
                $data[$k]['upd_time'] = '--:--:--';
            }

            //接受时间
            $ac_t = M('work_order_answer', '', 'KEFU')->where(['w_id' => $v['order_id']])->order('id asc')->limit(1)->field('ctime')->find();
            if (empty($ac_t['ctime'])) {
                $data[$k]['accept_time'] = '--:--:--';
            } else {
                $data[$k]['accept_time'] = date('Y-m-d H:i:s', $ac_t['ctime']);
            }
            //1一般，2加急，3紧急 类型： 1 普通 ， 2 技术 ，3 运营
            switch ($v['level']) {
                case 1:
                    $data[$k]['level'] = '一般';
                    break;
                case 2:
                    $data[$k]['level'] = '加急';
                    break;
                case 3:
                    $data[$k]['level'] = '紧急';
                    break;
            }
            switch ($v['type']) {
                case 1:
                    $data[$k]['type'] = '普通';
                    break;
                case 2:
                    $data[$k]['type'] = '技术';
                    break;
                case 3:
                    $data[$k]['type'] = '运营';
                    break;
            }
        }
        $data1 = $this->jiami(['code' => '1001', 'result' => 'success', 'page' => $page, 'is_null' => $is_null, 'data' => $data]);
        $this->ajaxReturn(['code' => 1001, 'data' => $data1]);
        die;
    }

    //工单处理详情
    public function work_order_detail()
    {

        $data_mm = I('data');
        $data_jm = $this->jiemi($data_mm);
        $order_id = $data_jm['order_id'];
        $where = [];
        $where['a.id'] = $order_id;
        $field = "a.id as order_id,a.publish_name,a.ctime,a.status,a.type,a.accept_apart_id,a.upd_time,a.level,b.content,b.img,c.name";
        $data = M('work_order', '', 'KEFU')->join('a join dxd_kefu.work_order_detail b on a.id=b.wo_id')->join('dxd_kefu.apart c on a.accept_apart_id=c.id ')->where($where)->field($field)->find();
        $data['ctime'] = date('Y-m-d H:i:s', $data['ctime']);
        if (!empty($data['upd_time'])) {
            $data['upd_time'] = date('Y-m-d H:i:s', $data['upd_time']);
        } else {
            $data['upd_time'] = '--:--:--';
        }
        $data['img'] = explode(',', $data['img']);

        //接受时间
        $ac_t = M('work_order_answer', '', 'KEFU')->where(['w_id' => $order_id])->order('id asc')->limit(1)->field('ctime')->find();
        if (empty($ac_t['ctime'])) {
            $data['accept_time'] = '--:--:--';
        } else {
            $data['accept_time'] = date('Y-m-d H:i:s', $ac_t['ctime']);
        }
        switch ($data['level']) {
            case 1:
                $data['level'] = '一般';
                break;
            case 2:
                $data['level'] = '加急';
                break;
            case 3:
                $data['level'] = '紧急';
                break;
        }
        switch ($data['type']) {
            case 1:
                $data['type'] = '普通';
                break;
            case 2:
                $data['type'] = '技术';
                break;
            case 3:
                $data['type'] = '运营';
                break;
        }

        $answer = M('work_order_answer', '', 'KEFU')->where(['w_id' => $order_id])->field('ctime,name,content,img as feedback_img')->order('id asc')->select();
        foreach ($answer as $k => $v) {
            $answer[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            $answer[$k]['feedback_img'] = explode(',', $v['feedback_img']);
        }
        $data['feedback'] = $answer;
        $data = $this->jiami(['code' => '1001', 'result' => 'success', 'data' => $data]);
        $this->ajaxReturn(['code' => '1001', 'data' => $data]);
        die;
    }

    //处理完结
    public function order_handle()
    {
        $data_mm = I('data');
        $data_jm = $this->jiemi($data_mm);
        $order_id = $data_jm['order_id'];
        $admin = $data_jm['admin_id'];
        $order = M('work_order', '', 'KEFU')->where(['id' => $order_id])->getField('publish_name');
        $admin_name = M('admin', '', 'KEFU')->where(['id' => $admin])->getField('name');
        if ($order != $admin_name) {
            $data = $this->jiami(['code' => '1002', 'result' => '发布人才可以完结工单！']);
            $this->ajaxReturn(['code' => '1001', 'data' => $data]);
            die;
        }
        $update = M('work_order', '', 'KEFU')->where(['id' => $order_id])->setField('status', 3);
        if ($update) {
            $data = $this->jiami(['code' => '1001', 'result' => '完结工单']);
        } else {
            $data = $this->jiami(['code' => '1002', 'result' => '完结工单失败']);
        }
        $this->ajaxReturn(['code' => '1001', 'data' => $data]);
    }

    //处理反馈
    public function order_feedback()
    {

        $data_mm = I('data');
        $data_jm = $this->jiemi($data_mm);
        $admin = $data_jm['admin_id'];
        $order_id = $data_jm['order_id'];
        $content = $data_jm['content'];
        $img = $data_jm['img'];
        $admin = M('admin', '', 'KEFU')->where(['id' => $admin])->field('id,part_id,name')->find();
        $push_adminid = M('work_order', '', 'KEFU')->join('a join dxd_kefu.admin b on a.publish_name=b.name')->where(['a.id' => $order_id])->getField('b.id');
        $arr = array('w_id' => $order_id, 'apart_id' => $admin['part_id'], 'name' => $admin['name'], 'content' => $content, 'img' => $img, 'ctime' => time());
        $add = M('work_order_answer', '', 'KEFU')->add($arr);
        if ($add) {
            $arr = array('status' => 2, 'upd_time' => time());
            M('work_order', '', 'KEFU')->where(['id' => $order_id])->save($arr);
            $data = $this->jiami(['code' => '1001', 'result' => '回复成功']);
            $app_key = 'dba87c6c10ab941f1e287026'; // appkey
            $master_secret = '0eb2ff195817d4b43915c2ac'; // master_secret
            $client = new \Admin\Service\JPush\Client($app_key, $master_secret);
            $pusher = $client->push();
            $pusher->setPlatform(array('ios', 'android'));
            $pusher->options(['apns_production' => true]);
            $pusher->addAlias(trim($push_adminid));
            $pusher->setNotificationAlert('您的工单有新的回复，请查看');
            try {
                $pusher->send();
            } catch (\Admin\Service\JPush\Exceptions\JPushException $e) {
                print $e;
            }
        } else {
            $data = $this->jiami(['code' => '1002', 'result' => '回复失败']);
        }
        $this->ajaxReturn(['code' => '1001', 'data' => $data]);
    }

    /**
     * 获取部门
     * */
    public function get_apart()
    {
        $data = M('apart', '', 'KEFU')->where(['status' => 1])->field('id,name')->select();
        $data = $this->jiami(['code' => '1001', 'result' => 'success', 'data' => $data]);
        $this->ajaxReturn(['code' => '1001', 'data' => $data]);
    }

    /**
     * 获取部门人员
     * */
    public function get_apart_name()
    {
        $apart = I('apart_id');
        switch ($apart) {
            case 1://客服
                $admin = M('admin', '', 'KEFU')->where(['role_id' => ['in', '1,2'], 'status' => 1])->field('id,name')->order('id desc')->select();
                break;
            case 2://运营 吴登密  王翎  陈秋利
                $admin = M('admin', '', 'USER')->where(['id' => ['in', '58,63,53'], 'status' => 1])->field('id,name')->select();
                break;
            case 4://风控
                $admin = M('fk_admin', '', 'KEFU_FK')->where(['id' => ['in', '1,6,7,8,13,14'], 'status' => 1])->field('id,name')->select();
                break;
            case 5://后端
                $admin = M('admin', '', 'USER')->where(['id' => ['in', '7,22,28,33,43'], 'status' => 1])->field('id,name')->select();
                break;
            case 6://贷后  王智  刘元芬
                $admin = M('admin', '', 'USER')->where(['id' => ['in', '27,39'], 'status' => 1])->field('id,name')->select();
                break;
            default:
                $admin = array();

                break;
        }
        $this->ajaxReturn(['code' => '1001', 'data' => $admin]);
    }

    //===================客服系统业务接口===========================
    // 工作认证不通过用户超过24小时，则转为信审
    public function work_cert_user()
    {
        $where = [];
        $where['status'] = 10;
        $where['kf_check_time'] = array('elt', time() - 24 * 3600);
        $data = M('anti_check_user', '', 'KEFU')->where($where)->field('out_kf_id,vid')->group('vid')->select();
        $arr = array();
        foreach ($data as $k => $v) {
            $arr[] = array('admin_name' => '客服系统', 'vid' => $v['vid'], 'content' => '用户超过24小时未对工作认证进行任何修改', 'ctime' => time());
        }
        $addall = M('tmp_user_mark', '', 'KEFU')->addAll($arr);
        if ($addall) {
            foreach ($data as $k => $v) {
                M('anti_check_user', '', 'KEFU')->where(['vid' => $v['vid']])->setField('status', 2);
                M('tmp_user', '', 'KEFU')->where(['vid' => $v['vid']])->setField('status', 2);
            }
        }
    }

    /**
     * 用户反馈问题  我们客服人员已经回复 但是客户没点完成  系统7天之后就自动完成
     */
    public function finish_feedback()
    {
        $where = [];
        $where['a.ctime'] = array('lt', time() - 7 * 3600 * 24);
        $data = M('user_feedback', '', 'KEFU')->join('a join dxd_kefu.user_message b on a.id=b.uf_id')->where($where)->getField('a.id', true);
        $w = [];
        $w['id'] = array('in', $data);
        M('user_feedback', '', 'KEFU')->where($w)->setField('status', 3);
        add_log('系统7天完结在线反馈');
    }

    //工单
    public function work_order_list1()
    {

        $status = I('status');//1 未完成  2 已完成
        $page = I('page');
        $part = I('part_id');
        $is_null = 0;
        $where = [];
        $w = [];
        $w['accept_apart_id'] = $part;
        $w['apart_id'] = $part;
        $w['_logic'] = "OR";
        if ($status == '2') {
            $where['status'] = array('in', array(3, 4));
        } else {
            $where['status'] = array('in', array(1, 2));
        }
        $where['_complex'] = $w;
        $where['_logic'] = "AND";
        $field = "id as order_id,publish_name,ctime,status,type,accept_apart_id,upd_time,level";
        $data = M('work_order', '', 'KEFU')->where($where)->limit($page, 10)->order('status asc')->field($field)->select();
        $page = $page + count($data);
        if (empty($data)) {
            $is_null = 1;
//            $this->ajaxReturn(['code' => '1001', 'result' => 'success', 'page' => $page, 'is_null' => $is_null, 'data' => $data]);
//            $json = json_encode("'code' => '1001', 'result' => 'success','page'=>$page,'is_null'=>$is_null ,'data' => $data");
            $this->ajaxReturn(['code' => '1001', 'result' => 'success', 'page' => $page, 'is_null' => $is_null, 'data' => $data]);
//            $this->ajaxReturn(['code' => 1001, 'data' => $data1]);
            die;
        }

        foreach ($data as $k => $v) {
            $data[$k]['accept_apart'] = M('apart', '', 'KEFU')->where(['id' => $v['accept_apart_id']])->getField('name');
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if (!empty($data[$k]['upd_time'])) {
                $data[$k]['upd_time'] = date('Y-m-d H:i:s', $v['upd_time']);
            } else {
                $data[$k]['upd_time'] = '--:--:--';
            }
            //接受时间
            $ac_t = M('work_order_answer', '', 'KEFU')->where(['w_id' => $v['order_id']])->order('id asc')->limit(1)->field('ctime')->find();
            if (empty($ac_t['ctime'])) {
                $data[$k]['accept_time'] = '--:--:--';
            } else {
                $data[$k]['accept_time'] = date('Y-m-d H:i:s', $ac_t['ctime']);
            }
            //1一般，2加急，3紧急 类型： 1 普通 ， 2 技术 ，3 运营
            switch ($v['level']) {
                case 1:
                    $data[$k]['level'] = '一般';
                    break;
                case 2:
                    $data[$k]['level'] = '加急';
                    break;
                case 3:
                    $data[$k]['level'] = '紧急';
                    break;
            }
            switch ($v['type']) {
                case 1:
                    $data[$k]['type'] = '普通';
                    break;
                case 2:
                    $data[$k]['type'] = '技术';
                    break;
                case 3:
                    $data[$k]['type'] = '运营';
                    break;
            }
        }
        $this->ajaxReturn(['code' => '1001', 'result' => 'success', 'page' => $page, 'is_null' => $is_null, 'data' => $data]);
        die;
    }


    /***
     * 注册未认证 转化统计
     */
    public function un_register()
    {
//        $admin=M('admin','','KEFU')->where(['role_id'=>2,'out_kf_id'=>0,'status'=>1,'part_id'=>1])->field('id,name')->select();
        $admin = M('admin', '', 'KEFU')->where(['id' => array('in', [69, 95, 934, 990,1000])])->field('id,name')->select();
        $arr = array();
        foreach ($admin as $k => $v) {
            $where = [];
            $where['a.admin_id'] = $v['id'];
            $where['b.admin'] = $v['name'];
            $where['a.type'] = 3;
            $where['b.calltype'] = 1;
            $where['b.state'] = 1;
            $where['b.ctime'] = array('between', array(1543593600, 1546272000));
            $un_cert = M('kefu_ownuser', '', 'KEFU')->join('a join dxd_kefu.call_info b on b.vid=a.vid')->join('join dxd_user.user c on c.vid=a.vid and c.cert_status!=1')->where($where)->field('b.ctime,b.vid,c.cert_time,c.ctime as register_time')->group('b.vid')->select();
            $count = 0;
            $no_count = 0;
            $day_count = 0;
            foreach ($un_cert as $k1 => $v1) {
                $date1 = date('Y-m-d', $v1['ctime']);
                $v1['cert_ctime']=M('user_examine','','ROM_DQSH')->where(['vid'=>$v1['vid']])->field('ctime')->find();
//                var_dump($c_time);die;
                $date2= date('Y-m-d', $v1['cert_ctime']);
                $date_register_time = date('Y-m-d', $v1['register_time']);
                $count++;
                if (!empty($v1['cert_ctime'])) {
                    if ($date2 == $date_register_time) {
                        $day_count++;
                    } else {
                        if ($date1 == $date2) {
                            $count++;
                        } else {
                            $no_count++;
                        }
                    }
                }
            }
            $count_arr['当天认证'] = $day_count;
            $count_arr['延期认证'] = $count;
            $count_arr['has_no_cert'] = $no_count;
            $arr[$v['name']] = $count_arr;
        }
//        $this->ajaxReturn($arr);
        $result=json_encode($arr);

        var_dump($result);
    }

    /***
     * 认证 转化统计
     */
    public function user_borrow()
    {
        $admin = M('admin', '', 'KEFU')->where(['id' => array('in', [69, 95, 939, 934, 990])])->field('id,name')->select();
//        $admin=M('admin','','KEFU')->where(['role_id'=>2,'out_kf_id'=>0,'status'=>1,'part_id'=>1])->field('id,name')->select();
        $arr = array();
        foreach ($admin as $k => $v) {
            $where = [];
            $where['a.admin_id'] = $v['id'];
            $where['b.admin'] = $v['name'];
            $where['a.type'] = 1;
            $where['b.calltype'] = 1;
            $where['b.state'] = 1;
            $where['d.ctime'] = array('between', array(1530374400, 1533052800));
            $where['b.ctime'] = array('between', array(1530374400, 1533052800));
            $un_cert = M('kefu_ownuser', '', 'KEFU')->join('a join dxd_kefu.call_info b on b.vid=a.vid')->join('join dxd_caiwu.user_borrow d on d.vid=a.vid and d.is_success=2')->join('join dxd_user.user c on c.vid=a.vid')->where($where)->field('b.ctime,b.vid,d.ctime as b_time,d.money')->group('b.vid')->select();
            $count = 0;
            $over_count = 0;
            $money = 0;
            foreach ($un_cert as $k1 => $v1) {
                $call_date = date('Y-m-d', $v1['ctime']);
                $borrow_date = date('Y-m-d', $v1['b_time']);
                if ($call_date == $borrow_date) {
                    $count++;
                    $get_money = intval($v1['money'] * 0.005, 2);
                    if ($get_money <= 10) {
                        $get_money = 10;
                    }
                    $money = $money + $get_money;
                } else {
                    $over_count++;
                    $money = $money + 1;
                }
            }
            $count_arr['当天借款'] = $count;//当天借款
            $count_arr['延迟借款'] = $over_count;//延迟借款
            $arr[$v['name']] = $count_arr;
//            $arr['money']=$money;
        }
        $re=json_encode($arr);

        var_dump($re);
    }

    /****
     * @param:
     * @param:
     * 还款接通
     */
    public function today_repay1()
    {
        $admin = M('admin', '', 'KEFU')->where(['id' => array('in', [69, 95, 941, 934, 957, 990])])->field('id,name')->select();
        $arr = array();
        foreach ($admin as $k => $v) {
            $i = 0;
            $day_count = 0;
            $delay_count = 0;
            while ($i <= 31) {
                $where = [];
                $where['a.repay_time'] = array('between', array(1533052800 + $i * 86400, 1533052800 + ($i + 1) * 86400));
                $where['b.ctime'] = array('between', array(1533052800 + $i * 86400, 1533052800 + ($i + 1) * 86400));
                $where['a.status'] = 3;
                $where['b.name'] = $v['name'];
                $field = 'a.real_time,a.repay_time,a.vid,b.ctime as call_time';
                $repayment = M('user_repayment', '', 'CAIWU_R')->join('a join dxd_kefu.temp_today_pay_mark b on b.vid=a.vid')->field($field)->group('b.vid')->where($where)->select();

                foreach ($repayment as $k1 => $v1) {
                    $call_time = date('Y-m-d', $v1['call_time']);
                    $real_pay_time = date('Y-m-d', $v1['real_time']);
                    if ($call_time == $real_pay_time) {
                        $day_count++;
                    } else if($v1['real_time']< $v1['call_time']* 86400 ) {
                        $delay_count++;
                    }
                }
                $i++;
            }
            $count_arr = array('day_count' => $day_count, 'delay_count' => $delay_count);
            $arr[$v['name']] = $count_arr;
        }
        $arr=json_encode($arr);
        var_dump($arr);
    }

    //在线反馈统计
    public function online_statistics()
    {
        $start='2018-07-01';
        $end='2018-08-01';
        $start_time=strtotime($start);
        $end_time=strtotime($end);
//        if(empty($start) && empty($end)){
//            $start_time=strtotime(date('Y-m-01',time()));
////            $end_time=strtotime(date('Y-m-t',time()));
//            $end_time=time();
//        }

        $admin=M('admin','','KEFU_R')->where(['id' => array('in', [69, 93, 94, 95, 939, 941, 934, 948, 957, 990])])->field('name,id')->select();
        foreach ($admin as $k=>$v){
            $where=[];
            $where['b.ctime']=array('between',array($start_time,$end_time));
            $where['a.vid']=array('eq','0');
            $where['a.admin_name']=array('eq',$v['name']);
            $field='b.ctime,a.ctime as back_time,b.vid,a.admin_name';
            $data=M('user_message','','KEFU_R')->join('a join dxd_kefu.user_feedback b on b.id=a.uf_id')->field($field)->where($where)->group('a.uf_id')->select();
            $one_hour=60*60;
            $over_count=0;// 超时回复计数
            $admin[$k]['total_feedback']=count($data);
            foreach ($data as $k1=>$v1){
                //休息时间不计入，午休 11;30-13：30,  下班：20:00
                $feedback_time=$v1['ctime'];
                $day_time=strtotime(date('Y-m-d',$v1['ctime']));//用户反馈当天凌晨
                //非上班时间
                $nine=$day_time+9*3600;//早上9点上班
                $lunch=$day_time+11*3600+1800;//中午休息
                $afternoon=$day_time+13*3600+1800;//中午休息
                $night=$day_time+20*3600;//晚班下班
                if($feedback_time<$nine){//上班九点之前的 相当于9点的反馈，
                    $feedback_time=$nine;
                }else  if($feedback_time> $lunch && $feedback_time< $afternoon){//午休时段的 相当于中午上班时候反馈
                    $feedback_time=$afternoon;
                }else  if($feedback_time>$night ){//晚班之后，相当于第二天9点的反馈
                    $feedback_time=$nine+24*3600;
                }

                if($feedback_time+$one_hour <=$v1['back_time']){
                    $over_count++;
                }
            }
            $admin[$k]['over_count']=$over_count;
        }
        var_dump($admin);
//        $this->assign(array(
//            'data'=>$admin,
//            'start_time'=>$start,
//            'end_time'=>$end,
//        ));
//        $this->display();
    }

    /**
     * 未认证抢呼
     */
    public function un_cert(){
        $data=M('kf_snatch','','KEFU')->where(['type'=>1,'ctime'=>['between',[1543593600,1546272000]]])->field('vid,admin,ctime')->select();

        $arr=['刘琳琳','沈阳','王丹娜','章婷','苏丹'];
        $arr1=[];
        foreach ($arr as $v1){
            $connt=0;
            foreach ( $data as $k=>$v){

                if($v['admin']==$v1){
                    $go_cert_time=M('user','','USER')->where(['vid'=>$v['vid']])->field('cert_time')->find();

                    //24小时内的认证
                    if($go_cert_time['cert_time']<(strtotime(date('Y-m-d',$v['ctime']+24*3600))) && !empty($go_cert_time['cert_time'])  && $v['ctime']<$go_cert_time['cert_time']){
                        $connt++;
                    }
                }
            }
            $arr1[$v1] = $connt;
        }
        $result=json_encode($arr1);

        var_dump($result);

    }
    /**
     * 未完成认证
     */
    public function unfinished_cert(){

        $data=M('kf_snatch','','KEFU')->join('a join dxd_user.user b on b.vid=a.vid ')->where(['a.type'=>2,'a.ctime'=>['between',[1543593600, 1546272000]],'b.cert_status'=>['in',[2,3]]])->field('b.cert_time,b.vid,a.admin,a.ctime')->select();

        $arr=['刘琳琳','沈阳','王丹娜','章婷','苏丹'];
        $arr1=[];
        foreach ($arr as $v1){
            $connt=0;
            foreach ( $data as $k=>$v){
                if($v['admin']==$v1){
                    //24小时内的认证
                    if($v['cert_time']<($v['ctime']+24*3600) && $v['ctime']<$v['cert_time']){
                        $connt++;
                    }
                }
            }
            $arr1[$v1] = $connt;
        }
        $result=json_encode($arr1);
        var_dump($result);

    }
    /**
     * 未借款
     */
    public function un_borrow(){

        $data=M('kf_snatch','','KEFU')
            ->join('a join dxd_caiwu.user_borrow b on b.vid=a.vid and b.is_success=2')
            ->join('left join dxd_kefu.kefu_ownuser c on c.vid=a.vid and c.type=1')
            ->where(['a.type'=>3,'c.vid'=>['exp','is null'],'a.ctime'=>['between',[1543593600, 1546272000]],'b.ctime'=>['between',[1543593600, 1546272000]]])
            ->field('b.ctime as b_time,b.vid,a.admin,a.ctime')
            ->group('a.vid')
            ->select();
//        var_dump($data);die;
        $arr=['刘琳琳','沈阳','王丹娜','章婷','苏丹'];
        $arr1=[];
        foreach ($arr as $v1){
            $connt=0;
                foreach ( $data as $k=>$v){
                if($v['admin']==$v1){
                    //24小时内的借款
                    if($v['b_time']<(strtotime(date('Y-m-d',$v['ctime']+24*3600))) && $v['b_time']>$v['ctime']){
                        $connt++;
                    }
                }
            }
            $arr1[$v1] = $connt;
        }
        $result=json_encode($arr1);
        var_dump($result);
    }


    public function get_location(){
//        $Ip = new \Org\Net\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
//        $area = $Ip->getlocation('119.176.119.51');
        $p=get_client_ip();
        $Ips = new  \Org\Net\OrgNetIpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
        $area = $Ips->getlocation($p); // 获取某个IP地址所在的位置
        var_dump($area);
    }

}
